From 9eafa32f4860b2634f0fac37f2987744f58bf299 Mon Sep 17 00:00:00 2001 From: "kfraser@localhost.localdomain" Date: Fri, 8 Dec 2006 15:59:30 +0000 Subject: [PATCH] [LIBXC] Make strerror() thread-safe by protecting it with a mutex. Using strerror_r() is made difficult by the different GNU definition, and different distros variously choose the POSIX or GNU prototype. Signed-off-by: Keir Fraser --- tools/libxc/Makefile | 4 ++-- tools/libxc/xc_private.c | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile index 129b867ff6..f92cde836d 100644 --- a/tools/libxc/Makefile +++ b/tools/libxc/Makefile @@ -119,7 +119,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(MAJOR).$(MINOR) ln -sf $< $@ libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS) - $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^ + $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^ -lpthread # libxenguest @@ -132,7 +132,7 @@ libxenguest.so.$(MAJOR): libxenguest.so.$(MAJOR).$(MINOR) ln -sf $< $@ libxenguest.so.$(MAJOR).$(MINOR): $(GUEST_PIC_OBJS) libxenctrl.so - $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz -lxenctrl + $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz -lxenctrl -lpthread -include $(DEPS) diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c index ed76cf003c..82e190989c 100644 --- a/tools/libxc/xc_private.c +++ b/tools/libxc/xc_private.c @@ -7,8 +7,8 @@ #include #include "xc_private.h" #include "xg_private.h" - #include +#include static __thread xc_error last_error = { XC_ERROR_NONE, ""}; #if DEBUG @@ -486,14 +486,20 @@ unsigned long xc_make_page_below_4G( char *safe_strerror(int errcode) { static __thread char errbuf[32]; -#ifdef __GLIBC__ - /* Broken GNU definition of strerror_r may not use our supplied buffer. */ - return strerror_r(errcode, errbuf, sizeof(errbuf)); -#else - /* Assume we have the POSIX definition of strerror_r. */ - strerror_r(errcode, errbuf, sizeof(errbuf)); + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + char *strerror_str; + + /* + * Thread-unsafe strerror() is protected by a local mutex. We copy + * the string to a thread-private buffer before releasing the mutex. + */ + pthread_mutex_lock(&mutex); + strerror_str = strerror(errcode); + strncpy(errbuf, strerror_str, sizeof(errbuf)); + errbuf[sizeof(errbuf)-1] = '\0'; + pthread_mutex_unlock(&mutex); + return errbuf; -#endif } /* -- 2.30.2